package com.edu.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysMenu;
import com.edu.admin.service.MenuService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.MenuVo;
import com.github.pagehelper.PageInfo;

/**
 * 菜单管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/menu", name = "菜单管理")
public class MenuController {
	
	@Autowired
	private MenuService	menuService;
	
    
    /**
	 *
	 * @apiGroup menu
	 * @api {get} /menu/list 根据学段获取年级列表
	 * @apiVersion 1.0.0
	 * @apiName getMenuList
	 *
	 * @apiParam {Long} phaseId 学段id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "gradeId": 7,
		            "gradeName": "七年级",
		            "phaseId": 2,
		            "yearNum": 1,
		            "phaseName": "初中"
		        },
		        {
		            "gradeId": 8,
		            "gradeName": "八年级",
		            "phaseId": 2,
		            "yearNum": 2,
		            "phaseName": "初中"
		        },
		        {
		            "gradeId": 9,
		            "gradeName": "九年级",
		            "phaseId": 2,
		            "yearNum": 3,
		            "phaseName": "初中"
		        }
		    ]
		}
	 *
	 *
	 */
   @RequestMapping(value = "listPage", name = "获取所有菜单列表(分页)", method = RequestMethod.GET)
   @PreAuthorize("hasAuthority('/menu/listPage')")
   public RestResult getMenuListPage(@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize)  throws Exception {

	   PageInfo<MenuVo> pageInfo = menuService.getMenuListPage(pageNum,pageSize);
       return RestResult.ok(pageInfo);
   }
    
   
    /**
	 *
	 * @apiGroup menu
	 * @api {get} /menu/menuMap 按产品层级获取菜单结构
	 * @apiVersion 1.0.0
	 * @apiName getMenuListByRoleLevel
	 *
	 * @apiParam {Integer} productLevel 产品层级
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "menuParentIds": [
		            1
		        ],
		        "menuList": [
		            {
		                "menuId": 1,
		                "menuName": "基础设置",
		                "menuUrls": "\\",
		                "requestUrls": "",
		                "menuParentId": 0,
		                "productId": 1,
		                "orderNum": 0,
		                "sonMenuList": [
		                    {
		                        "menuId": 2,
		                        "menuName": "学段管理",
		                        "menuUrls": "\\phase",
		                        "requestUrls": "",
		                        "menuParentId": 1,
		                        "productId": 1,
		                        "orderNum": 0,
		                        "sonMenuList": []
		                    }
		                ]
		            }
		        ]
		    }
		}
	 *
	 *
	 */
   @RequestMapping(value = "menuMap", name = "角色菜单列表", method = RequestMethod.GET)
   @PreAuthorize("hasAuthority('/menu/menuMap')")
   public RestResult getMenuListByRoleLevel(@RequestParam(name = "productLevel") Integer productLevel,
		   					@RequestParam(name = "roleLevel") Integer roleLevel) throws Exception {
   	Map<String, Object> menuMap = new HashMap<>();
   	List<MenuVo> menuList = menuService.getMenuListByRoleLevel(productLevel,roleLevel);
   	menuMap.put("menuList",menuList);
   	List<Long> menuParentIds = new ArrayList<Long>();
   	menuService.getMenuParentList(menuList,menuParentIds);
   	menuMap.put("menuParentIds",menuParentIds);
   	return RestResult.ok(menuMap);
   }
   
   /**
	 *
	 * @apiGroup menu
	 * @api {get} /menu/menuMapBySchoolId 按学校id获取该校所有产品的菜单结构
	 * @apiVersion 1.0.0
	 * @apiName getMenuListBySchoolId
	 *
	 * @apiParam {Long} schoolId 学校ID
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "menuParentIds": [
		            1
		        ],
		        "menuList": [
		            {
		                "menuId": 1,
		                "menuName": "基础设置",
		                "menuUrls": "\\",
		                "requestUrls": "",
		                "menuParentId": 0,
		                "productId": 1,
		                "orderNum": 0,
		                "sonMenuList": [
		                    {
		                        "menuId": 2,
		                        "menuName": "学段管理",
		                        "menuUrls": "\\phase",
		                        "requestUrls": "",
		                        "menuParentId": 1,
		                        "productId": 1,
		                        "orderNum": 0,
		                        "sonMenuList": []
		                    }
		                ]
		            }
		        ]
		    }
		}
	 *
	 *
	 */
   @RequestMapping(value = "menuMapBySchoolId", name = "按学校id获取该校所有产品的菜单结构", method = RequestMethod.GET)
   @PreAuthorize("hasAuthority('/menu/menuMapBySchoolId')")
   public RestResult getMenuListBySchoolId(@RequestParam(name = "productLevel") Integer productLevel,
		   							@RequestParam(name = "schoolId") Long schoolId) throws Exception {
   	Map<String, Object> menuMap = new HashMap<>();
   	List<MenuVo> menuList = menuService.getMenuListBySchoolId(productLevel,schoolId);
   	menuMap.put("menuList",menuList);
   	List<Long> menuParentIds = new ArrayList<Long>();
   	menuService.getMenuParentList(menuList,menuParentIds);
   	menuMap.put("menuParentIds",menuParentIds);
   	return RestResult.ok(menuMap);
   }
    

   /**
	 *
	 * @apiGroup menu
	 * @api {post} /menu/save 保存菜单信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysMenu} menu 菜单信息
	 * @apiParamExample 请求示例（json）
		 {
		    "menuName": "学段管理",
		    "menuUrls": "\\phase",
		    "requestUrls": "",
		    "menuParentId": 1,
		    "productId": "1"
		
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "menuId": 3
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "save", name = "保存菜单信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/menu/save')")
	public RestResult save(@Valid @RequestBody SysMenu menu) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = menuService.saveMenu(menu);
	
		return RestResult.ok(result);
	}
	

	/**
	 *
	 * @apiGroup menu
	 * @api {get} /menu/info 获取菜单信息
	 * @apiVersion 1.0.0
	 * @apiName getMenu
	 *
	 * @apiParam {Long} menuId 菜单id
	 *  
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "menuId": 2,
		        "menuName": "校园管理",
		        "menuUrls": "",
		        "requestUrls": "",
		        "menuParentId": 1,
		        "productId": 1,
		        "orderNum": 0,
		        "sonMenuList": null
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "菜单信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/menu/info')")
	public RestResult getMenu(@RequestParam Long menuId) throws Exception{
		SysMenu menu = null;
		MenuVo menuVo = null;
		
		menu = menuService.selectByPrimaryKey(menuId);
		
		if (menu!=null)
		{
			menuVo = new MenuVo();
			BeanUtils.copyProperties(menu,menuVo);
		}

		return RestResult.ok(menuVo);
	}


}
